<?php

namespace app\admin\controller;

//订单管理
class Order extends Common
{
    //订单列表
    public function order_list()
    {
        $where = array('a.o_id' => array('gt', 0));
        $mobile = Request()->param('mobile');
        //注册手机号
        $order_num = Request()->param('order_num');
        //订单编号
        $status = Request()->param('status');
        //状态
        $type = Request()->param('type');
        //收货方式
        $pay_type = Request()->param('pay_type');
        //支付方式
        $pay_status = Request()->param('pay_status');
        //支付状态
        $start_time = strtotime(Request()->param('start_time'));
        $end_time = strtotime(Request()->param('end_time'));
        $day = Request()->param('day');
        $page = Request()->param('page');
        $size = Request()->param('size');
        if ($page < 1) {
            $page = 1;
        }
        if ($size < 15) {
            $size = 15;
        }
        $now = ($page - 1) * $size;
        if ($mobile != '') {
            $where['b.u_mobile'] = array('like', '%' . $mobile . '%');
        }
        if ($order_num != '') {
            $where['a.o_order_num'] = array('like', '%' . $order_num . '%');
        }
        if ($status != 0) {
            $where['a.o_status'] = $status;
            if($status == 2){
                $where['a.o_status'] = 1;
                $where['a.o_examine'] = 2;
            }
        } else {
            $where['a.o_status'] = array('neq',0);
            $status = 0;
        }
        if($type>0){
            $where['a.o_type'] = $type;
        }else{
            $type=0;
        }
        if($pay_type>0){
            $where['a.o_pay_type'] = $pay_type;
        }else{
            $pay_type=0;
        }
        if($pay_status>0){
            if($pay_status==1){
                $where['a.o_pay_status'] = 1;
            }else{
                $where['a.o_pay_status'] = array('neq',1);
            }

        }else{
            $pay_status=0;
        }
        $where_time = array(array('gt', 0), array('gt', 0));
        if ($start_time > 0) {
            $where_time[] = array('egt', $start_time);
        } else {
            $start_time = '';
        }
        if ($end_time > 0) {
            $where_time[] = array('elt', $end_time);
        } else {
            $end_time = '';
        }
        $where['a.o_create_time'] = $where_time;
        if ($day != '') {
            $where['a.o_delivery_time'] = array('like', '%' . $day . '%');
        }
        $order = db('order a')
            ->field("a.*,b.u_mobile,b.u_real_name,b.u_store_name")
            ->join('fr_user b', 'a.u_id = b.u_id', 'left')
            ->order('a.o_id desc')
            ->where($where)
            ->limit("$now,$size")
            ->select();
        //标签列表
        $count = db('order a')
            ->join('fr_user b', 'a.u_id = b.u_id', 'left')
            ->where($where)
            ->count();
        //等级信息
        $page_str = page($page, $size, $count, count($order));//分页
        $this->view->assign('page_str', $page_str);
        $this->view->assign('data', $order);
        $this->assign('status', $status);
        $this->assign('type', $type);
        $this->assign('pay_type', $pay_type);
        $this->assign('pay_status', $pay_status);
        $this->assign('mobile', $mobile);
        $this->assign('order_num', $order_num);
        $this->assign('start_time', $start_time);
        $this->assign('end_time', $end_time);
        $this->assign('day', $day);
        $this->assign('page', $page);
        $this->assign('size', $size);
        $this->assign('count', $count);
        return $this->view->fetch();

    }

    //订单商品列表
    public function goods_list()
    {
        $where = array('a.o_id' => array('gt', 0));
        $mobile = Request()->param('mobile');
        //注册手机号
        $order_num = Request()->param('order_num');
        //订单编号
        $status = Request()->param('status');
        //状态
        $start_time = strtotime(Request()->param('start_time'));
        $end_time = strtotime(Request()->param('end_time'));
        $day = Request()->param('day');
        $page = Request()->param('page');
        $size = Request()->param('size');
        if ($page < 1) {
            $page = 1;
        }
        if ($size < 15) {
            $size = 15;
        }
        $now = ($page - 1) * $size;
        if ($mobile != '') {
            $where['b.u_mobile'] = array('like', '%' . $mobile . '%');
        }
        if ($order_num != '') {
            $where['b.o_order_num'] = array('like', '%' . $order_num . '%');
        }
        if ($status > 0) {
            $where['a.o_status'] = $status;
        } else {
            $where['a.o_status'] = array('gt', 0);
            $status = 0;
        }
        $where_time = array(array('gt', 0), array('gt', 0));
        if ($start_time > 0) {
            $where_time[] = array('egt', $start_time);
        } else {
            $start_time = '';
        }
        if ($end_time > 0) {
            $where_time[] = array('elt', $end_time);
        } else {
            $end_time = '';
        }
        $where['a.o_create_time'] = $where_time;
        if ($day != '') {
            $where['a.o_delivery_time'] = array('like', '%' . $day . '%');
        }
        $order = db('order a')
            ->field("c.*,b.u_mobile,a.o_order_num,a.o_status,a.o_delivery_time,a.o_create_time")
            ->join('fr_user b', 'a.u_id = b.u_id', 'left')
            ->join('fr_order_goods c', 'a.o_id = c.o_id', 'left')
            ->order('a.o_id desc')
            ->where($where)
            ->limit("$now,$size")
            ->select();
        //标签列表
        $count = db('order a')
            ->join('fr_user b', 'a.u_id = b.u_id', 'left')
            ->join('fr_order_goods c', 'a.o_id = c.o_id', 'left')
            ->where($where)
            ->count();
        //等级信息
        $page_str = page($page, $size, $count, count($order));//分页
        $this->view->assign('page_str', $page_str);
        $this->view->assign('data', $order);
        $this->assign('status', $status);
        $this->assign('mobile', $mobile);
        $this->assign('order_num', $order_num);
        $this->assign('start_time', $start_time);
        $this->assign('end_time', $end_time);
        $this->assign('day', $day);
        $this->assign('page', $page);
        $this->assign('size', $size);
        $this->assign('count', $count);
        return $this->view->fetch();

    }

    //待收货订单
    function processing_list()
    {
        $where = array('a.o_id' => array('gt', 0));
        $mobile = Request()->param('mobile');
        //注册手机号
        $order_num = Request()->param('order_num');
        //订单编号
        $status = 1;
        //状态:1：待收货；
        $type = Request()->param('type');
        //收货方式
        $pay_type = Request()->param('pay_type');
        //支付方式
        $pay_status = Request()->param('pay_status');
        //支付状态
        $start_time = strtotime(Request()->param('start_time'));
        $end_time = strtotime(Request()->param('end_time'));
        $day = Request()->param('day');
        $page = Request()->param('page');
        $size = Request()->param('size');
        if ($page < 1) {
            $page = 1;
        }
        if ($size < 15) {
            $size = 15;
        }
        $now = ($page - 1) * $size;
        if ($mobile != '') {
            $where['b.u_mobile'] = array('like', '%' . $mobile . '%');
        }
        if ($order_num != '') {
            $where['b.o_order_num'] = array('like', '%' . $order_num . '%');
        }
        if($type>0){
            $where['a.o_type'] = $type;
        }else{
            $type=0;
        }
        if($pay_type>0){
            $where['a.o_pay_type'] = $pay_type;
        }else{
            $pay_type=0;
        }
        if($pay_status>0){
            if($pay_status==1){
                $where['a.o_pay_status'] = 1;
            }else{
                $where['a.o_pay_status'] = array('neq',1);
            }

        }else{
            $pay_status=0;
        }

        $where['a.o_status'] = $status;
        $where_time = array(array('gt', 0), array('gt', 0));
        if ($start_time > 0) {
            $where_time[] = array('egt', $start_time);
        } else {
            $start_time = '';
        }
        if ($end_time > 0) {
            $where_time[] = array('elt', $end_time);
        } else {
            $end_time = '';
        }
        $where['a.o_create_time'] = $where_time;
        if ($day != '') {
            $where['a.o_delivery_time'] = array('like', '%' . $day . '%');
        }
        $order = db('order a')
            ->field("a.*,b.u_mobile,b.u_real_name,b.u_store_name")
            ->join('fr_user b', 'a.u_id = b.u_id', 'left')
            ->order('a.o_id desc')
            ->where($where)
            ->limit("$now,$size")
            ->select();
        //标签列表
        $count = db('order a')
            ->join('fr_user b', 'a.u_id = b.u_id', 'left')
            ->where($where)
            ->count();
        //等级信息
        $page_str = page($page, $size, $count, count($order));//分页
        $this->view->assign('page_str', $page_str);
        $this->view->assign('data', $order);
        $this->assign('type', $type);
        $this->assign('pay_type', $pay_type);
        $this->assign('pay_status', $pay_status);
        $this->assign('mobile', $mobile);
        $this->assign('order_num', $order_num);
        $this->assign('start_time', $start_time);
        $this->assign('end_time', $end_time);
        $this->assign('day', $day);
        $this->assign('page', $page);
        $this->assign('size', $size);
        $this->assign('count', $count);
        return $this->view->fetch();
    }
    //已完成订单
    //待收货订单
    function success_list()
    {
        $where = array('a.o_id' => array('gt', 0));
        $mobile = Request()->param('mobile');
        //注册手机号
        $order_num = Request()->param('order_num');
        //订单编号
        $type = Request()->param('type');
        //收货方式
        $pay_type = Request()->param('pay_type');
        //支付方式
        $pay_status = Request()->param('pay_status');
        //支付状态
        $start_time = strtotime(Request()->param('start_time'));
        $end_time = strtotime(Request()->param('end_time'));
        $day = Request()->param('day');
        $page = Request()->param('page');
        $size = Request()->param('size');
        if ($page < 1) {
            $page = 1;
        }
        if ($size < 15) {
            $size = 15;
        }
        $now = ($page - 1) * $size;
        if ($mobile != '') {
            $where['b.u_mobile'] = array('like', '%' . $mobile . '%');
        }
        if ($order_num != '') {
            $where['b.o_order_num'] = array('like', '%' . $order_num . '%');
        }
        if($type>0){
            $where['a.o_type'] = $type;
        }else{
            $type=0;
        }
        if($pay_type>0){
            $where['a.o_pay_type'] = $pay_type;
        }else{
            $pay_type=0;
        }
        if($pay_status>0){
            if($pay_status==1){
                $where['a.o_pay_status'] = 1;
            }else{
                $where['a.o_pay_status'] = array('neq',1);
            }

        }else{
            $pay_status=0;
        }

        $where['a.o_status'] = array('in', '10,11');
        $where_time = array(array('gt', 0), array('gt', 0));
        if ($start_time > 0) {
            $where_time[] = array('egt', $start_time);
        } else {
            $start_time = '';
        }
        if ($end_time > 0) {
            $where_time[] = array('elt', $end_time);
        } else {
            $end_time = '';
        }
        $where['a.o_create_time'] = $where_time;
        if ($day != '') {
            $where['a.o_delivery_time'] = array('like', '%' . $day . '%');
        }
        $order = db('order a')
            ->field("a.*,b.u_mobile,b.u_real_name,b.u_store_name")
            ->join('fr_user b', 'a.u_id = b.u_id', 'left')
            ->order('a.o_id desc')
            ->where($where)
            ->limit("$now,$size")
            ->select();
        //标签列表
        $count = db('order a')
            ->join('fr_user b', 'a.u_id = b.u_id', 'left')
            ->where($where)
            ->count();
        //等级信息
        $page_str = page($page, $size, $count, count($order));//分页
        $this->view->assign('page_str', $page_str);
        $this->view->assign('data', $order);
        $this->assign('type', $type);
        $this->assign('pay_type', $pay_type);
        $this->assign('pay_status', $pay_status);
        $this->assign('mobile', $mobile);
        $this->assign('order_num', $order_num);
        $this->assign('start_time', $start_time);
        $this->assign('end_time', $end_time);
        $this->assign('day', $day);
        $this->assign('page', $page);
        $this->assign('size', $size);
        $this->assign('count', $count);
        return $this->view->fetch();
    }

    //退货申请
    function return_list()
    {
        $where = array('a.or_id' => array('gt', 0));
        $order_num = Request()->param('order_num');
        //订单编号
        $status = Request()->param('status');
        //状态
        $start_time = strtotime(Request()->param('start_time'));
        $end_time = strtotime(Request()->param('end_time'));
        $page = Request()->param('page');
        $size = Request()->param('size');
        if ($page < 1) {
            $page = 1;
        }
        if ($size < 15) {
            $size = 15;
        }
        $now = ($page - 1) * $size;
        if ($order_num != '') {
            $where['b.o_order_num'] = array('like', '%' . $order_num . '%');
        }
        if ($status > 0) {
            $where['a.or_status'] = $status;
        }
        $where_time = array(array('gt', 0), array('gt', 0));
        if ($start_time > 0) {
            $where_time[] = array('egt', $start_time);
        } else {
            $start_time = '';
        }
        if ($end_time > 0) {
            $where_time[] = array('elt', $end_time);
        } else {
            $end_time = '';
        }
        $where['a.or_create_time'] = $where_time;
        $return = db('order_return a')
            ->field("a.*,b.o_order_num,b.o_real_price,b.o_create_time,b.o_end_time,b.o_user_name,b.o_user_mobile,b.o_address,c.u_mobile")
            ->join("fr_order b", "a.o_id = b.o_id", "left")
            ->join('fr_user c', 'b.u_id = c.u_id', 'left')
            ->where($where)
            ->limit("$now,$size")
            ->order("a.or_id desc")
            ->select();
        $count = db('order_return a')
            ->join("fr_order b", "a.o_id = b.o_id", "left")
            ->join('fr_user c', 'b.u_id = c.u_id', 'left')
            ->where($where)
            ->count();
        //等级信息
        $page_str = page($page, $size, $count, count($return));//分页
        $this->assign('data', $return);
        $this->assign('count', $count);
        $this->assign('page_str', $page_str);
        $this->assign('status', $status);
        $this->assign('order_num', $order_num);
        $this->assign('start_time', $start_time);
        $this->assign('end_time', $end_time);
        $this->assign('page', $page);
        $this->assign('size', $size);
        return $this->view->fetch();
    }

    //开始处理
    function start_handle()
    {
        if (request()->isAjax()) {
            $id = request()->post('id');
            $return = db('order_return a')
                ->where(array('or_id' => $id))
                ->find();
            if ($return['or_status'] != 1) {
                return return_json(array('code' => 0, 'msg' => '已经开始了'), '开始处理退货');
            }
            $res = db('order_return a')
                ->where(array('or_id' => $id))
                ->update(array('or_status' => 2));
            if ($res !== false) {
                return return_json(array('code' => 1, 'msg' => '开始成功'), '开始处理退货');
            } else {
                return return_json(array('code' => 0, 'msg' => '开始失败'), '开始处理退货');
            }
        }
    }

    //处理退货
    function return_handle()
    {
        if (request()->isGet()) {
            $id = request()->get('id');
            if($id>0){
                $return = db('order_return a')
                    ->field("a.*,b.o_order_num,b.o_real_price,b.o_create_time,b.o_end_time,b.o_user_name,b.o_user_mobile,b.o_address,c.u_mobile")
                    ->join("fr_order b", "a.o_id = b.o_id", "left")
                    ->join('fr_user c', 'b.u_id = c.u_id', 'left')
                    ->where(array('a.or_id' => $id))
                    ->find();

            }else{
                $o_id = request()->get('o_id');
                $return = db('order_return a')
                    ->field("a.*,b.o_order_num,b.o_real_price,b.o_create_time,b.o_end_time,b.o_user_name,b.o_user_mobile,b.o_address,c.u_mobile")
                    ->join("fr_order b", "a.o_id = b.o_id", "left")
                    ->join('fr_user c', 'b.u_id = c.u_id', 'left')
                    ->where(array('a.o_id' => $o_id))
                    ->find();
            }

            $this->assign('return', $return);
            return $this->view->fetch();
        } else {
            $data = request()->post();
            $id = $data['id'];
            $or_status = $data['or_status'];
            $is_back = $data['is_back'];
            $is_reduce = $data['is_reduce'];
            $or_reply = $data['or_reply'];
            $return = db('order_return a')
                ->field("a.*,b.o_add_integral,b.u_id,b.o_status,b.o_order_num")
                ->join("fr_order b", "a.o_id = b.o_id", "left")
                ->where(array('a.or_id' => $id))
                ->find();
            if (empty($return)) {
                return return_json(array('code' => 0, 'msg' => '没有该申请'), '处理退货');
            }
            if ($return['o_status']!=9) {
                return return_json(array('code' => 0, 'msg' => '订单为非退货状态'), '处理退货');
            }
            db()->startTrans();
            //开启事务
            if ($is_reduce == 1) {
                $user=db('user a')
                    ->where(array('u_id'=>$return['u_id']))
                    ->find();
                $old_integral=$user['u_integral'];
                $new_integral= $old_integral-$return['o_add_integral'];
                if($new_integral<0){
                    $new_integral=0;
                }
                $res0 =db('user')
                    ->where(array('u_id'=>$return['u_id']))
                    ->update(array('u_integral'=>$new_integral));
                $data1=array('u_id'=>$return['u_id'],'old_integral'=>$old_integral,'new_integral'=>$new_integral,'ui_integral'=>$return['o_add_integral'],'o_id'=>$return['o_id'],'o_order_num'=>$return['o_order_num'],'ui_status'=>2,'ui_type'=>3,'ui_create_time'=>time(),'ui_add_user_id'=>session('uid'),'ui_add_user_name'=>session('user.real_name'));
                db('user_integral')->insertGetId($data1);
            } else {
                $res0 = 1;
            }
            if ($or_status == 2) {
                $res1 = db('order_return a')
                    ->where(array('a.or_id' => $id))
                    ->update(array('or_status' => 4,'order_end_time'=>time(), 'or_reply' => $or_reply,'or_admin_id'=>session('uid'),'or_admin_user_name'=>session('user.real_name')));
                $res2 = db('order a')
                    ->where(array('a.o_id' => $return['o_id']))
                    ->update(array('o_status' => 10));
                if ($res0!==false && $res1 !== false && $res2 !== false) {
                    db()->commit();
                    return return_json(array('code' => 1, 'msg' => '处理成功'), '处理退货');
                } else {
                    db()->rollback();
                    return return_json(array('code' => 0, 'msg' => '处理失败'), '处理退货');
                }
            } else if ($or_status == 1) {
                $res1 = db('order_return a')
                    ->where(array('a.or_id' => $id))
                    ->update(array('or_status' => 3,'order_end_time'=>time(), 'or_reply' => $or_reply,'or_admin_id'=>session('uid'),'or_admin_user_name'=>session('user.real_name')));
                $res2 = db('order a')
                    ->where(array('a.o_id' => $return['o_id']))
                    ->update(array('o_status' => 11));
                if ($is_back == 1) {
                    $order_goods = db('order_goods')
                        ->where(array('o_id' => $return['o_id']))
                        ->select();
                    foreach ($order_goods as $k => $v) {
                        $goods = db('goods_norm a')
                            ->field("a.*,b.g_sales")
                            ->join("fr_goods b", "a.g_id = b.g_id", "left")
                            ->where(array('gn_id' => $v['gn_id']))
                            ->find();
                        $new_stock = $goods['gn_stock'] + $v['g_number'];
                        $gn_sales = $goods['gn_sales'] - $v['g_number'];
                        $res4 = db('goods_norm a')
                            ->where(array('gn_id' => $v['gn_id']))
                            ->update(array('gn_stock' => $new_stock, 'gn_sales' => $gn_sales));
                        $res5 = db('goods')
                            ->where(array('g_id' => $v['g_id']))
                            ->setDec('g_sales', $v['g_number']);
                        if ($res4 !== false && $res5 !== false) {
                            $res3 = 1;
                        } else {
                            $res3 = false;
                        }
                    }
                } else {
                    $res3 = 1;
                }
                if ($res0!==false && $res1 !== false && $res2 !== false && $res3 !== false) {
                    db()->commit();
                    db('order_log')->insert(array('o_id'=>$return['o_id'],'type'=>2,'uid'=>session('uid'),'user_name'=>session('user.real_name'),'create_time'=>time(),'action'=>'处理退货订单：成功退货，回复：'.$or_reply));
                    return return_json(array('code' => 1, 'msg' => '处理成功'), '处理退货');
                } else {
                    db()->rollback();
                    db('order_log')->insert(array('o_id'=>$return['o_id'],'type'=>2,'uid'=>session('uid'),'user_name'=>session('user.real_name'),'create_time'=>time(),'action'=>'处理退货订单：取消退货，回复：'.$or_reply));
                    return return_json(array('code' => 0, 'msg' => '处理失败'), '处理退货');
                }
            }
        }
    }

    //订单详情
    function order_details()
    {
        if (request()->isGet()) {
            $id = request()->get('id');
            $order = db('order a')
                ->field("a.*,b.u_mobile")
                ->join('fr_user b', 'a.u_id = b.u_id', 'left')
                ->where(array('o_id' => $id))
                ->find();
            $goods = db('order_goods a')
                ->field("a.*,b.g_img")
                ->join("fr_goods b","a.g_id = b.g_id","left")
                ->where(array('a.o_id' => $id))
                ->order("a.og_id")
                ->select();
            $return=db('order_return')
                ->where(array('o_id'=>$id))
                ->find();
            $log=db('order_log')
                ->where(array('o_id'=>$id))
                ->select();
            $this->assign('order', $order);
            $this->assign('goods', $goods);
            $this->assign('return', $return);
            $this->assign('log', $log);
            return $this->view->fetch();
        }
    }
    //订单详情
    function printing()
    {
        if (request()->isGet()) {
            $id = request()->get('id');
            $order = db('order a')
                ->field("a.*,b.u_mobile")
                ->join('fr_user b', 'a.u_id = b.u_id', 'left')
                ->where(array('o_id' => $id))
                ->find();
            $goods = db('order_goods a')
                ->field("a.*,b.g_img")
                ->join("fr_goods b","a.g_id = b.g_id","left")
                ->where(array('a.o_id' => $id))
                ->order("a.og_id")
                ->select();
            foreach($goods as $k=>$v){
                $goods[$k]['gn_details_price']=round($v['gn_price']/$v['gn_details_number'],2).'/'.$v['gn_details_unit'];
            }
            $return=db('order_return')
                ->where(array('o_id'=>$id))
                ->find();
            $log=db('order_log')
                ->where(array('o_id'=>$id))
                ->select();
            $this->assign('order', $order);
            $this->assign('goods', $goods);
            $this->assign('return', $return);
            $this->assign('log', $log);
            return $this->view->fetch();
        }
    }

    //导出订单+商品
    function out_order_list()
    {
        $status = request()->get('status');
        $type = request()->get('type');
        $pay_type = request()->get('pay_type');
        $pay_status = request()->get('pay_status');
        $mobile = request()->get('mobile');
        $order_num = request()->get('order_num');
        $start_time = request()->get('start_time');
        $end_time = request()->get('end_time');
        if ($mobile != '') {
            $where['b.u_mobile'] = array('like', '%' . $mobile . '%');
        }
        if ($order_num != '') {
            $where['b.o_order_num'] = array('like', '%' . $order_num . '%');
        }
        if ($status > 0) {
            $where['a.o_status'] = $status;
        } else {
            if ($status == 'success') {
                $where['a.o_status'] = array('egt', 10);
            } else {
                $where['a.o_status'] = array('neq',0);
            }

        }
        if($type>0){
            $where['a.o_type'] = $type;
        }else{
            $type=0;
        }
        if($pay_type>0){
            $where['a.o_pay_type'] = $pay_type;
        }else{
            $pay_type=0;
        }
        if($pay_status>0){
            if($pay_status==1){
                $where['a.o_pay_status'] = 1;
            }else{
                $where['a.o_pay_status'] = array('neq',1);
            }

        }else{
            $pay_status=0;
        }
        $where_time = array(array('gt', 0), array('gt', 0));
        if ($start_time > 0) {
            $where_time[] = array('egt', $start_time);
        }
        if ($end_time > 0) {
            $where_time[] = array('elt', $end_time);
        }
        $where['a.o_create_time'] = $where_time;
        $order = db('order a')
            ->field("a.*,b.u_mobile")
            ->join('fr_user b', 'a.u_id = b.u_id', 'left')
            ->order('a.o_id desc')
            ->where($where)
            ->select();
        if (!empty($order)) {
            $header = array(
                '订单编号',
                '提货方式',
                '收货人/自提人员',
                '收货人电话/自提人员电话',
                '收货地址/自提地址',
                '联系人电话',
                '下单时间',
                '提货时间',
                '支付金额',
                '支付方式',
                '是否支付',
                '状态',
                '商品总价',
                '运费',
                '优惠券金额',
                '备注',
                '商品名称',
                '商品规格',
                '商品数量',
                '商品单价',
                '商品单重',
                '商品总价',
            );
            foreach ($order as $k => $v) {
                if ($v['o_type'] == 1) {
                    $o_type = '配送上门';
                    $user_mobile=$v['o_user_mobile'];
                } else if ($v['o_type'] == 2) {
                    $o_type = '自提';
                    $user_mobile=$v['user_mobile'];
                }
                if ($v['o_pay_type'] == 1) {
                    $o_pay_type = '微信';
                } else if ($v['o_pay_type'] == 2) {
                    $o_pay_type = '支付宝';
                } else if ($v['o_pay_type'] == 3) {
                    $o_pay_type = '货到付款';
                }else{
                    $o_pay_type = '未选择支付方式';
                }
                if ($v['o_pay_status'] == 1) {
                    $o_pay_status = '已支付';
                } else if ($v['o_pay_status'] != 1) {
                    $o_pay_status = '未支付';
                }
                if ($v['o_status'] == -1) {
                    $o_status = '已取消';
                }else if ($v['o_status'] == 1) {
                    $o_status = '待收货';
                } else if ($v['o_status'] == 10) {
                    $o_status = '已收货';
                } else if ($v['o_status'] == 9) {
                    $o_status = '申请退货';
                } else if ($v['o_status'] == 11) {
                    $o_status = '已退货';
                }
                $data[] = array(
                    $v['o_order_num'],
                    $o_type,
                    $v['o_user_name'],
                    $v['o_user_mobile'],
                    $v['o_address'],
                    $user_mobile,
                    date('Y-m-d-H:i:s', $v['o_create_time']),
                    $v['o_delivery_time'],
                    $v['o_real_price'],
                    $o_pay_type,
                    $o_pay_status,
                    $o_status,
                    $v['o_all_price'],
                    $v['o_freight'],
                    $v['uc_discount'],
                    $v['o_remarks'],
                );
                $goods = db('order_goods')
                    ->where(array('o_id' => $v['o_id']))
                    ->order("g_id")
                    ->select();
                if (!empty($goods)) {
                    foreach ($goods as $vv) {
                        $data1 = array('', '', '', '', '', '', '', '', '', '', '', '', '', '', '','');
                        $data1[] = $vv['g_name'];
                        $data1[] = $vv['gn_name'];
                        $data1[] = $vv['g_number'];
                        $data1[] = $vv['gn_price'];
                        $data1[] = $vv['gn_weight'] . 'kg';
                        $data1[] = $vv['og_all_price'];
                        $data[] = $data1;
                    }

                }
                $data[] = array();
                $data[] = array();
            }
            createtable($data, $header, '订单+商品' . date('Y-m-d-H-i-s'));
        } else {
            $header = array('没有数据');
            $data[] = array('没有数据');
            createtable($data, $header, '订单+商品' . date('Y-m-d-H-i-s'));
        }
    }
    //导出订单
    function out_order_list1()
    {
        $status = request()->get('status');
        $type = request()->get('type');
        $pay_type = request()->get('pay_type');
        $pay_status = request()->get('pay_status');
        $mobile = request()->get('mobile');
        $order_num = request()->get('order_num');
        $start_time = request()->get('start_time');
        $end_time = request()->get('end_time');
        if ($mobile != '') {
            $where['b.u_mobile'] = array('like', '%' . $mobile . '%');
        }
        if ($order_num != '') {
            $where['b.o_order_num'] = array('like', '%' . $order_num . '%');
        }
        if ($status > 0) {
            $where['a.o_status'] = $status;
        } else {
            if ($status == 'success') {
                $where['a.o_status'] = array('egt', 10);
            } else {
                $where['a.o_status'] = array('neq',0);
            }

        }
        if($type>0){
            $where['a.o_type'] = $type;
        }else{
            $type=0;
        }
        if($pay_type>0){
            $where['a.o_pay_type'] = $pay_type;
        }else{
            $pay_type=0;
        }
        if($pay_status>0){
            if($pay_status==1){
                $where['a.o_pay_status'] = 1;
            }else{
                $where['a.o_pay_status'] = array('neq',1);
            }

        }else{
            $pay_status=0;
        }
        $where_time = array(array('gt', 0), array('gt', 0));
        if ($start_time > 0) {
            $where_time[] = array('egt', $start_time);
        }
        if ($end_time > 0) {
            $where_time[] = array('elt', $end_time);
        }
        $where['a.o_create_time'] = $where_time;
        $order = db('order a')
            ->field("a.*,b.u_mobile,b.u_real_name,b.u_store_name")
            ->join('fr_user b', 'a.u_id = b.u_id', 'left')
            ->order('a.o_id desc')
            ->where($where)
            ->select();
        if (!empty($order)) {
            $header = array(
                'ID',
                '订单号',
                '用户(店铺)',
                '注册手机号',
                '收货人',
                '收货人电话',
                '支付金额',
                '支付方式',
                '支付状态',
                '配送/提货时间',
                '收货方式',
                '创建时间',
                '状态',
            );
            foreach ($order as $k => $v) {
                if($v['o_pay_type']==0){
                    $o_pay_type='未选择';
                }else if($v['o_pay_type']==1){
                    $o_pay_type='微信';
                }else if($v['o_pay_type']==2){
                    $o_pay_type='支付宝';
                }else if($v['o_pay_type']==3){
                    $o_pay_type='货到付款';
                }
                //支付方式
                if($v['o_pay_status']==1){
                    $o_pay_status='已付款';
                }else if($v['o_pay_status']!=1){
                    $o_pay_status='未付款';
                }
                //支付状态
                if($v['o_type']==1){
                    $o_type='配送';
                }else if($v['o_type']==2){
                    $o_type='自提';
                }
                //收货方式
                if($v['o_status']==-1){
                    $o_status='已取消';
                }else if($v['o_status']==1){
                    $o_status='待收货';
                }else if($v['o_status']==10){
                    $o_status='已收货';
                }else if($v['o_status']==9){
                    $o_status='申请退货';
                }else if($v['o_status']==11){
                    $o_status='退货完成';
                }
                $data[]=array(
                    $v['o_id'],
                    $v['o_order_num'],
                    $v['u_real_name'].'('.$v['u_store_name'].')',
                    $v['u_mobile'],
                    $v['o_user_name'],
                    $v['o_user_mobile'],
                    $v['o_real_price'],
                    $o_pay_type,
                    $o_pay_status,
                    $v['o_delivery_time'],
                    $o_type,
                    date('Y-m-d H:i:s',$v['o_create_time']),
                    $o_status,
                );
            }
            createtable($data, $header, '订单' . date('Y-m-d-H-i-s'));
        } else {
            $header = array('没有数据');
            $data[] = array('没有数据');
            createtable($data, $header, '订单' . date('Y-m-d-H-i-s'));
        }
    }
    //货到付款管理员处理
    function order_handle(){
        if (request()->isGet()) {
            $id = request()->get('id');
            $order = db('order a')
                ->field("a.*,b.u_mobile")
                ->join('fr_user b', 'a.u_id = b.u_id', 'left')
                ->where(array('o_id' => $id))
                ->find();
            $goods = db('order_goods')
                ->where(array('o_id' => $id))
                ->order("g_id")
                ->select();
            $this->assign('order', $order);
            $this->assign('goods', $goods);
            $this->assign('id', $id);
            return $this->view->fetch();
        }else if(request()->isAjax()){
            $id = request()->post('id');
            $status=request()->post('status');
            $remark=request()->post('remark');
            $order = db('order a')
                ->field("a.*,b.u_mobile")
                ->join('fr_user b', 'a.u_id = b.u_id', 'left')
                ->where(array('o_id' => $id))
                ->find();
            if(empty($order) || $order['o_status']!=1 || $order['o_pay_type']!=3){
                return return_json(array('code' => 0, 'msg' => '该订单不是未收货的货到付款订单'), '处理货到付款');
            }
            if($remark==''){
                return return_json(array('code' => 0, 'msg' => '请输入处理备注'), '处理货到付款');
            }
            if($status==2){
                //取消订单
                db()->startTrans();
                db("order")
                    ->where(array('o_id' => $id))
                    ->update(array('o_status' => -1,'o_end_time'=>time(),'o_is_admin_handle'=>1,'o_admin_user_id'=>session('uid'),'o_admin_name'=>session('user.real_name'),'o_handle_remark'=>$remark));
                $return=$this->automatic_order_cancel($order);
                if($return['code']==1){
                    db()->commit();
                    db('order_log')->insert(array('o_id'=>$id,'type'=>2,'uid'=>session('uid'),'user_name'=>session('user.real_name'),'create_time'=>time(),'action'=>'处理货到付款：取消订单，备注：'.$remark));
                    return return_json(array('code' => 1, 'msg' => '处理货到付款成功'), '处理货到付款');
                }else{
                    db()->rollback();
                    return return_json(array('code' => 0, 'msg' => '处理货到付款失败'), '处理货到付款');
                }
            }else if($status==1){
                db()->startTrans();
                $res=db('order')
                    ->where(array('o_id' => $id))
                    ->update(array('o_end_time'=>time(),'o_status'=>10,'o_pay_status'=>1,'o_pay_time'=>time(),'o_is_admin_handle'=>1,'o_admin_user_id'=>session('uid'),'o_admin_name'=>session('user.real_name'),'o_handle_remark'=>$remark));
                $user=db('user')->where(array('u_id'=>$order['u_id']))->find();
                $this->give_integral($order,$user);
                if ($res != false) {
                    db()->commit();
                    db('order_log')->insert(array('o_id'=>$id,'type'=>2,'uid'=>session('uid'),'user_name'=>session('user.real_name'),'create_time'=>time(),'action'=>'处理货到付款：收货完成，备注：'.$remark));
                    return return_json(array('code' => 1, 'msg' => '处理货到付款成功'), '处理货到付款');
                } else {
                    db()->rollback();
                    return return_json(array('code' => 0, 'msg' => '处理货到付款失败'), '处理货到付款');
                }
            }
        }
    }
    //处理审核
    function order_examine(){
        if (request()->isGet()) {
            $id = request()->get('id');
            $order = db('order a')
                ->field("a.*,b.u_mobile")
                ->join('fr_user b', 'a.u_id = b.u_id', 'left')
                ->where(array('o_id' => $id))
                ->find();
            $goods = db('order_goods')
                ->where(array('o_id' => $id))
                ->order("g_id")
                ->select();
            $this->assign('order', $order);
            $this->assign('goods', $goods);
            $this->assign('id', $id);
            return $this->view->fetch();
        }else if(request()->isAjax()){
            $id = request()->post('id');
            $status=request()->post('status');
            $remark=request()->post('remark');
            $order = db('order a')
                ->field("a.*,b.u_mobile")
                ->join('fr_user b', 'a.u_id = b.u_id', 'left')
                ->where(array('o_id' => $id))
                ->find();
            if(empty($order) || $order['o_status']!=1 || $order['o_examine']!=2){
                return return_json(array('code' => 0, 'msg' => '该订单不是未收货的待审核订单'), '处理审核');
            }
            if($remark==''){
                return return_json(array('code' => 0, 'msg' => '请输入处理备注'), '处理审核');
            }
            if($status==2){
                //取消订单
                db()->startTrans();
                db("order")
                    ->where(array('o_id' => $id))
                    ->update(array('o_status' => -1,'o_end_time'=>time(),'o_examine'=>-1,'o_examine_admin_id'=>session('uid'),'o_examine_admin_name'=>session('user.real_name'),'o_examine_remark'=>$remark));
                $return=$this->automatic_order_cancel($order);
                if($return['code']==1){
                    db()->commit();
                    db('order_log')->insert(array('o_id'=>$id,'type'=>2,'uid'=>session('uid'),'user_name'=>session('user.real_name'),'create_time'=>time(),'action'=>'处理审核订单：取消订单，备注：'.$remark));
                    return return_json(array('code' => 1, 'msg' => '处理审核成功'), '处理审核');
                }else{
                    db()->rollback();
                    return return_json(array('code' => 0, 'msg' => '处理审核失败:'.$return['msg']), '处理审核');
                }
            }else if($status==1){
                db()->startTrans();
                $res=db('order')
                    ->where(array('o_id' => $id))
                    ->update(array('o_end_time'=>time(),'o_examine'=>1,'o_examine_admin_id'=>session('uid'),'o_examine_admin_name'=>session('user.real_name'),'o_examine_remark'=>$remark));
                if ($res != false) {
                    db()->commit();
                    db('order_log')->insert(array('o_id'=>$id,'type'=>2,'uid'=>session('uid'),'user_name'=>session('user.real_name'),'create_time'=>time(),'action'=>'处理审核订单：审核通过，备注：'.$remark));
                    return return_json(array('code' => 1, 'msg' => '处理审核成功'), '处理审核');
                } else {
                    db()->rollback();
                    return return_json(array('code' => 0, 'msg' => '处理审核失败'), '处理审核');
                }
            }
        }
    }
    /**
     * 自动取消订单
     */
    public function automatic_order_cancel($order)
    {
        $o_id=$order['o_id'];
        if (empty($order)) {
            return array('code' => 2, 'msg' => '订单出错', 'data' => array());
        }
        if ($order['o_status'] != 1) {
//            || $order['o_pay_status'] == 1
            return array('code' => 2, 'msg' => '该订单不可取消');
        }
        $res1 = 1;
        $goods = db("order_goods")
            ->where(array("o_id" => $order['o_id']))
            ->select();
        foreach ($goods as $v) {
            $res2 = db('goods_norm a')
                ->where(array('gn_id' => $v['gn_id']))
                ->setDec("gn_sales", $v['g_number']);
            //减少规格销量
            $res3 = db('goods_norm a')
                ->where(array('gn_id' => $v['gn_id']))
                ->setInc("gn_stock", $v['g_number']);
            //增加规格库存
            $res4 = db('goods a')
                ->where(array('g_id' => $v['g_id']))
                ->setDec("g_sales", $v['g_number']);
            //减少商品销量
            $res5 = 1;
            //对应添加订单商品
            $rush_goods = db('rush_goods a')
                ->field("a.*")
                ->join("fr_rush b", "a.r_id=b.r_id", "left")
                ->where("a.g_id = " . $v['g_id'] . " and a.rg_status=1 and a.rg_sales < a.rg_all and b.r_date = '" . date('Y-m-d') . "' and b.r_start_time <= " . time() . " and b.r_end_time > " . time() . " and b.r_status = 1")
                ->find();
            //限时抢购商品
            if (!empty($rush_goods)) {
                if ($rush_goods['rg_sales'] - $v['g_number'] < $rush_goods['rg_all']) {
                    //还可以继续抢购
                    $all = $rush_goods['rg_sales'] - $v['g_number'];
                    db('rush_goods a')
                        ->where(array('rg_id' => $rush_goods['rg_id']))
                        ->update(array('rg_sales' => $all, 'is_full' => 0));
                    //减少已抢购量
                } else {
                    //已抢购满了
                    $all = $rush_goods['rg_sales'] - $v['g_number'];
                    db('rush_goods a')
                        ->where(array('rg_id' => $rush_goods['rg_id']))
                        ->update(array('rg_sales' => $all, 'is_full' => 1));
                    //全部抢购完
                }

            }
            if ($res2 == false || $res3 == false || $res4 == false || $res5 <= 0) {
                break;
            }
        }
        if ($order['uc_id'] > 0) {
            $res6 = db('user_coupon a')
                ->where(array('uc_id' => $order['uc_id']))
                ->update(array('o_id' => 0, 'uc_status' => 1,'back_time'=>time()));
            //消耗优惠券
            db('user_coupon_log')->insertGetId(array('ucl_create_time' => time(), 'uc_id' => $order['uc_id'], 'u_id' => $order['u_id'], 'c_num' => 1, 'c_type' => 5, 'o_id' => $res1, 'o_order_num' => $order['o_order_num']));
            //增加优惠券记录
        } else {
            $res6 = 1;
        }
        if ($res1 > 0 && $res2 != false && $res3 != false && $res4 != false && $res5 > 0 && $res6 !== false) {
            return array('code' => 1, 'msg' => '取消成功');
        } else {
            return array('code' => 2, 'msg' => '取消失败');
        }
    }
    /**
     * 赠送积分
     */
    public function give_integral($order,$user)
    {
        $old_integral=$user['u_integral'];
        $integral=$order['o_add_integral'];
        $status=1;
        $new_integral= $old_integral+$integral;
        $res1=db('user a')
            ->where(array('u_id'=>$user['u_id'],'u_status'=>1))
            ->update(array('u_integral'=>$new_integral));
        $data1=array('u_id'=>$user['u_id'],'o_id'=>$order['o_id'],'o_order_num'=>$order['o_order_num'],'old_integral'=>$old_integral,'new_integral'=>$new_integral,'ui_integral'=>$integral,'ui_status'=>$status,'ui_type'=>1,'ui_create_time'=>time());
        $res2=db('user_integral')->insertGetId($data1);

    }
}
